//
//  Dialog.swift
//  OnemeetingDemo
//
//  Created by 张衡 on 2019/12/6.
//  Copyright © 2019 www.onemeeting.1meetingIos. All rights reserved.
//

import Foundation

typealias UpBtnBlock = ()->Void


enum DSAppearance {
    case light
    case dark
}

class Dialog: NSView {
    
    static let shared = Dialog()

    var kDiaWidth     = CGFloat(300)
    var kDHeight      = CGFloat(110)
    
    var touchBlock: UpBtnBlock?
    var dismissBlock: os_block_t?
    
    var current:DSAppearance = .dark {
        didSet {
            let shadowColor = current == .dark ? color_gray(60):color_gray(200)
            self.bgContainer.layer?.shadowColor = shadowColor.cgColor
            
            let borderColor =  current == .dark ? color_gray(0):color_gray(180)
            self.container.layer?.borderColor = borderColor.cgColor
        }
    }
    
    var isHide:Bool = true
    
    var isMove:Bool = false
    var movePoint:CGPoint = CGPoint.zero
    
    var bgClose:Bool = false
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
    }
    
    required init?(coder decoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var containerWidth:CGFloat? {
        didSet {
            if containerWidth != nil {
                self.kDiaWidth = containerWidth!
                self.bgContainer.snp.updateConstraints { (make) in
                    make.width.equalTo(containerWidth!)
                }
            }
        }
    }
    
    func show(relativeTo otherView:NSView?) {
        let shadowColor = current == .dark ? color_gray(60):color_gray(200)
        self.bgContainer.layer?.shadowColor = shadowColor.cgColor
        
        let borderColor =  current == .dark ? color_gray(0):color_gray(180)
        self.container.layer?.borderColor = borderColor.cgColor
        
        guard let kView = NSApplication.shared.mainWindow?.contentView else {
            return
        }
        kView.addSubview(self, positioned: NSWindow.OrderingMode.above, relativeTo: otherView)
        self.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        self.isHide = false
    }
    
    func setup(content:NSView) {
        self.addSubview(tapView)
        tapView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        tapView.addSubview(bgContainer)
        bgContainer.snp.makeConstraints { (make) in
            make.size.equalTo(content.bounds.size)
            make.center.equalToSuperview()
        }
        
        bgContainer.addSubview(container)
        container.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(NSEdgeInsetsMake(5, 5, 5, 5))
        }
        
        container.addSubview(content)
        content.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    func setupUI() {
        self.addSubview(maskView)
        maskView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        maskView.addSubview(bgContainer)
        bgContainer.snp.makeConstraints { (make) in
            make.width.equalTo(kDiaWidth)
            make.height.equalTo(300)
            make.center.equalToSuperview()
        }
        
        bgContainer.addSubview(container)
        container.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(NSEdgeInsetsMake(7, 7, 7, 7))
        }
        
    }
    
    /////以下定制版更新内容
    lazy var texView: BaseTextView = {
        let view = BaseTextView.init(frame: CGRect.zero)
        return view
    }()
    
    lazy var cacalBtn: ZHButton = {
        let btn = ZHButton()
        btn.zh_title = "取消".localized()
        btn.zh_font = NSFont.systemFont(ofSize: 14)
        btn.zh_color = NSColor.black
        btn.zh_backgroundColor = NSColor.white
        btn.backgroundHoverColor = color_gray(200)
        btn.hasBorder = true
        btn.zh_cornerRadius = 6
        btn.zh_borderWidth = 0.5
        btn.zh_borderColor = color_gray(200)
        
        btn.action = #selector(cacalBtnAction)
        btn.target = self
        return btn
    }()
    
    lazy var updataBtn: ZHButton = {
        let btn = ZHButton()
        btn.zh_title = "更新".localized()
        btn.zh_font = NSFont.systemFont(ofSize: 14)
        btn.zh_color = NSColor.white
        btn.zh_backgroundColor = color_main140
        btn.hasBorder = true
        btn.zh_cornerRadius = 6
        btn.action = #selector(updateBtnAction)
        btn.target = self
        return btn
    }()
    
    @objc func updateBtnAction() -> () {
        self.dismiss()
        self.touchBlock!()
    }
    
    @objc func cacalBtnAction() -> () {
        self.dismiss()
    }
    
    /////以下初始化内容
    lazy var versionLab: NSLabel = {
        let view = NSLabel.label(title: "1.0.1"+"版本更新内容".localized(), textColor: NSColor.black, font: NSFont.boldSystemFont(ofSize: 16))
        view.alignment = .left
        return view
    }()
    
   
    lazy var sTitle: NSLabel = {
        let view = NSLabel.label(title: "新版本啦！", textColor: color_gray(230), font: NSFont.systemFont(ofSize: 18))
        view.alignment = .left
        return view
    }()
    
    lazy var closeBtn: ZHButton = {
        let btn = ZHButton.init()
        btn.normalImage = NSImage.image(name:"icon_navtigate_close")!
        btn.zh_backgroundColor = NSColor.clear
        btn.target = self
        btn.action = #selector(closeBtnAction(senber:))
        return btn
    }()
    
    
    lazy var container: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(255).cgColor
        view.layer?.cornerRadius = 5
        view.layer?.borderColor = color_gray(0).cgColor
        view.layer?.borderWidth = 1
        view.layer?.masksToBounds = true
        return view
    }()
    
    lazy var bgContainer: MSView = {
        let view = MSView()
        view.mouseDelegate = self
        view.wantsLayer = true
        //view.shadow = NSShadow.init()
        //view.layer?.shadowOffset = NSSize.init(width: 0, height: -3)
        //view.layer?.shadowColor = color_gray(60).cgColor
        //view.layer?.shadowRadius = 20
        //view.layer?.shadowOpacity = 1
        //view.layer?.cornerRadius = 10
        return view
    }()
    
    lazy var maskView: MdView = {
        let view = MdView()
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(255,alpha:0.01).cgColor
        return view
    }()
    
    lazy var tapView: MSView = {
        let view = MSView()
        view.tap = { _ in
            self.dismiss()
        }
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(255,alpha:0.01).cgColor
        return view
    }()
    
    func initlize(title:String?) {
        self.setupUI()
        self.bgContainer.addSubview(sTitle)
        sTitle.snp.makeConstraints { (make) in
            make.height.equalTo(26)
            make.left.equalTo(30)
            make.right.equalToSuperview().offset(-30)
            make.top.equalToSuperview().offset(30)
        }
        
        self.bgContainer.addSubview(closeBtn)
        closeBtn.snp.makeConstraints { (make) in
            make.width.equalTo(20)
            make.height.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalToSuperview().offset(30)
        }
        
        self.kDHeight = CGFloat(60)
        let newT = title != nil ? title!:""
        self.sTitle.stringValue = newT
    }
    
    func addCont(view:NSView,height:CGFloat) {
        self.kDHeight+=height
        self.container.addSubview(view)
        view.snp.makeConstraints { (make) in
            make.top.equalTo(kDHeight-height)
            make.bottom.equalTo(-20)
            make.right.equalToSuperview().offset(-30)
            make.left.equalTo(30)
        }
    }
    
    lazy var content: DialogCont = {
        let view = DialogCont.init(frame: NSRect.zero)
        return view
    }()
    
    func show(title:String,
              msg:String?,
              confirmBtnTitle:String,
              confirmAction:BtnTapBlock?=nil,
              cacalBtnTitle:String,
              cacalAction:BtnTapBlock?=nil) {
        Dialog.shared.initlize(title: title)
        Dialog.shared.containerWidth = CGFloat(300)
        if msg != nil {
            let height = msg!.ga_Size(with: 14).height
            self.content.contentLab.stringValue = msg!
            self.content.confirmAction = confirmAction
            self.content.cacalAction = cacalAction
            self.content.confirmBtn.zh_title = confirmBtnTitle
            self.content.cacalBtn.zh_title = cacalBtnTitle
            self.content.contentLab.snp.updateConstraints { (make) in
                make.height.equalTo(height+120)
            }
            Dialog.shared.addCont(view: self.content, height: height+120)
        }else {
            Dialog.shared.addCont(view: NSView(), height:0)
        }
        Dialog.shared.show(inView: nil)
    }
    
    
    func show(inView:NSView?=nil) {
        
        self.bgContainer.snp.updateConstraints { (make) in
            make.height.equalTo(kDHeight)
        }
        
        var kView:NSView!
        if inView == nil {
            kView = NSApplication.shared.mainWindow?.contentView
        }else {
            kView = inView!
        }
        
        kView.addSubview(self, positioned: NSWindow.OrderingMode.above, relativeTo: inView)
        self.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    func dismiss() {
        //self.maskView.removeFromSuperview()
        self.tapView.removeFromSuperview()
        self.bgContainer.removeFromSuperview()
        self.content.removeFromSuperview()
        self.removeFromSuperview()
        if self.dismissBlock != nil {
            self.dismissBlock!()
        }
    }
    
    @objc func closeBtnAction(senber:ZHButton) {
        self.dismiss()
    }
    
    
}
extension Dialog:MouseDelegate {
    func mouseDelegate_mouseDown(view: NSView, event: NSEvent) {
        self.bgContainer.layoutSubtreeIfNeeded()
        self.movePoint = bgContainer.gcenter()
    }
    
    func mouseDelegate_mouseUp(view: NSView, event: NSEvent) {
        //self.bgContainer.layoutSubtreeIfNeeded()
        //self.movePoint = bgContainer.zh_center()
    }
    
    func mouseDelegate_mouseDragged(view: NSView, currntPoint: NSPoint) {
        if self.isMove == true {
            self.bgContainer.layoutSubtreeIfNeeded()
            var curPoint = currntPoint
            
            
            guard let sceenH = self.bgContainer.window?.contentView?.frame.size.height,let sceenW = self.bgContainer.window?.contentView?.frame.size.width else {
                return
            }
            
            let maxY = (sceenH-self.bgContainer.frame.height/2)
            let minY = self.bgContainer.frame.height/2
            let maxX = (sceenW-self.bgContainer.frame.width/2)
            let minX = self.bgContainer.frame.width/2
            
            if curPoint.y > maxY {
                curPoint.y = maxY
            }else if curPoint.y < minY {
                curPoint.y = minY
            }else if curPoint.x > maxX {
                curPoint.x = maxX
            }else if curPoint.x < minX {
                curPoint.x = minX
            }
            
            let x = curPoint.x-self.movePoint.x
            let y = (sceenH-curPoint.y)-self.movePoint.y
            
            self.bgContainer.snp.updateConstraints { (make) in
                make.centerX.equalTo(x)
                make.centerY.equalTo(y)
            }
        }
    }
    
    
}

extension Dialog {
    
    func showUpdata(title:String?=nil,version:String?,msg:String?,touchUp:UpBtnBlock? = nil) {
        
        self.setupUI()
        self.bgContainer.addSubview(sTitle)
        sTitle.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-40)
            make.height.equalTo(30)
            make.left.equalTo(40)
            make.top.equalToSuperview().offset(60)
        }
        
        touchBlock = touchUp

        let keyView = NSApplication.shared.mainWindow?.contentView

        let t = title==nil ? "":title!
        let m = msg==nil ? "":msg!
        let v = version==nil ? "":version!

        self.sTitle.stringValue = t
        self.texView.txView.string = m
        self.versionLab.stringValue = v+"版本更新内容".localized()
        self.cacalBtn.title = "取消".localized()
        self.updataBtn.title = "更新".localized()
        
        bgContainer.addSubview(versionLab)
        versionLab.snp.makeConstraints { (make) in
            make.top.equalTo(kDHeight)
            make.width.equalToSuperview().offset(-80)
            make.height.equalTo(22)
            make.left.equalTo(43)
        }
        
        bgContainer.addSubview(texView)
        texView.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-80)
            make.height.equalTo(55)
            make.top.equalTo(versionLab.snp.bottom).offset(10)
            make.left.equalTo(43)
        }

        bgContainer.addSubview(updataBtn)
        updataBtn.snp.makeConstraints { (make) in
            make.width.equalTo(60)
            make.right.equalTo(-40)
            make.height.equalTo(32)
            make.bottom.equalTo(-40)
        }
        
        bgContainer.addSubview(cacalBtn)
        cacalBtn.snp.makeConstraints { (make) in
            make.width.equalTo(60)
            make.right.equalTo(updataBtn.snp.left).offset(-15)
            make.height.equalTo(32)
            make.bottom.equalTo(-40)
        }
       
        //diaView高度
        bgContainer.snp.updateConstraints { (make) in
            make.height.equalTo(CGFloat(300))
        }
        
        keyView?.addSubview(self, positioned: NSWindow.OrderingMode.above, relativeTo: keyView)
        self.snp.makeConstraints { (make) in
           make.edges.equalToSuperview()
        }
   }
}
